package GUI;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JTabbedPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;

import Client.HTSMain;

import javax.swing.ImageIcon;

import java.awt.Cursor;

public class AddScenario extends Menu {
	
	public static String StepID;
	
	private int col; 
	private int row;
	public static Vector<Vector<Object>> data=null;
	private JTextArea ExpectedtextArea=null;
	private JTextArea ActualtextArea=null;
	private JTextArea TestStepTextArea=null;
	private JTextArea CommentTextArea=null;
	private JTextArea TestDescriptiontextArea=null;
	
	public AddScenario(ArrayList<Object> msg) {
		AddScenarioFrame.setLocation(642, 0);
			
		initComponents(msg);	
	}

	
	

	public void initComponents(final ArrayList<Object> str)
	{
		Menu.btnRun.setEnabled(false); //setting buttons
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(56, 472, 401, 195);
		add(scrollPane_1);
		
		
		ExpectedtextArea = new JTextArea();
		ExpectedtextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		scrollPane_1.setViewportView(ExpectedtextArea);
		
		JLabel lblExpected = new JLabel("Expected:");
		lblExpected.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblExpected.setBounds(56, 447, 76, 24);
		add(lblExpected);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(504, 472, 401, 195);
		add(scrollPane_2);
		
		ActualtextArea = new JTextArea();
		ActualtextArea.setEnabled(false);
		ActualtextArea.setEditable(false);
		ActualtextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		scrollPane_2.setViewportView(ActualtextArea);
		
		JLabel lblActual = new JLabel("Actual:");
		lblActual.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblActual.setBounds(504, 451, 62, 17);
		add(lblActual);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setFont(new Font("Tahoma", Font.PLAIN, 14));
		tabbedPane.setBounds(56, 277, 850, 163);
		add(tabbedPane);
		
		TestStepTextArea = new JTextArea();
		TestStepTextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		tabbedPane.addTab("Test Step", null, TestStepTextArea, null);
		
		TestDescriptiontextArea = new JTextArea();
		TestDescriptiontextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		tabbedPane.addTab("Test Description", null, TestDescriptiontextArea, null);
		
		CommentTextArea = new JTextArea();
		CommentTextArea.setEnabled(false);
		CommentTextArea.setEditable(false);
		CommentTextArea.setFont(new Font("Courier New", Font.PLAIN, 14));
		tabbedPane.addTab("Comment", null, CommentTextArea, null);
		
		
		
		
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(56, 103, 850, 163);
		add(scrollPane);

		data = new Vector<Vector<Object>>();
		 if (str.size() >3)
		 {
		 for (int i=2; i<=(str.size()-3);i=i+3){
		        Vector<Object> row = creatrow(str,i);
		        data.add(row);
		        }
    
		 }
        
	
        Vector<String> headers = new Vector<String>();
        headers.add("Step ID");
        headers.add("Description");
        headers.add("Status");
        final JTable table = new JTable( data, headers );
        table.setFont(new Font("Tahoma", Font.PLAIN, 14));
        table.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		 col = table.getSelectedColumn();   
                 row = table.getSelectedRow();
                boolean is=table.isColumnSelected(0);
                
                if (!is){
        			
        			JOptionPane.showMessageDialog(null, "Please select Step ID only","Error",JOptionPane.ERROR_MESSAGE);

        		}
                else // mouse listener
                {
                	//Menu.btnRun.setEnabled(true);                           
                	
                	StepID=((String)table.getValueAt(row, col));
            		
                	ArrayList AddArray=new ArrayList();
    	 			AddArray.add("LoadStep");
    	 			AddArray.add(StepID);
    	 			AddArray.add(str.get(1));
    	 			
    	 		//	HTSMain.chat.handleMessageFromGui(AddArray);
            		
                }
        		
        	}
        });
       // table.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(true);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        JTableHeader header = table.getTableHeader();
        header.setDefaultRenderer(centerRenderer);
        for (int i=0;i<table.getColumnCount();i++)
        {
        	table.getColumnModel().getColumn(i).setCellRenderer( centerRenderer );
        	
        }
        table.setRowHeight(20);
        table.setPreferredScrollableViewportSize(new Dimension(900,150));
        scrollPane.setViewportView(table);
        
        JLabel SaveLabel = new JLabel("");
        SaveLabel.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) { // confirm step
        		    
        	    ArrayList<Object> AddArray=new ArrayList<Object>();
	 			AddArray.add("SaveStep");
	 			AddArray.add(str.get(1));
	 			AddArray.add(ExpectedtextArea.getText());
	 			AddArray.add(ActualtextArea.getText());
	 			AddArray.add(TestStepTextArea.getText());
	 			AddArray.add(CommentTextArea.getText());
	 			AddArray.add(TestDescriptiontextArea.getText());
	 			HTSMain.chat.handleMessageFromGui(AddArray);
	 			
        	}
        });
        SaveLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));        
        SaveLabel.setIcon(new ImageIcon(AddScenario.class.getResource("/Images/04_Save_48x48.png")));
        SaveLabel.setBounds(928, 575, 76, 92);
        add(SaveLabel);
        
        
     
        	SaveLabel.enable(true);
    
        
		
		
	} 
	
	private Vector<Object> creatrow(ArrayList<Object>str, int i) {
		 Vector<Object> row = new Vector<Object>();
   row.add( ""+str.get(i));
   row.add( ""+str.get(i+1));
   row.add( ""+str.get(i+2));

		return row;
	}
	
	

}
